package com.lbmb.mdps.settings.service.impl;

import com.lbmb.mdps.commons.domain.EmployeeReceive;
import com.lbmb.mdps.commons.domain.EmployeeVo;
import com.lbmb.mdps.commons.domain.UserReceive;
import com.lbmb.mdps.commons.domain.WarehouseVo;
import com.lbmb.mdps.commons.exception.EmployeeException;
import com.lbmb.mdps.commons.utils.Md5Util;
import com.lbmb.mdps.settings.dao.EmployeeDao;
import com.lbmb.mdps.settings.domain.Employee;
import com.lbmb.mdps.settings.service.EmployeeService;
import com.lbmb.mdps.workbench.domain.Warehouse;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * @author 林文塨
 * @date 2022-05-30 9:00
 * description
 */
@Service("employeeService")
public class EmployeeServiceImpl implements EmployeeService {

    @Resource
    private EmployeeDao employeeDao;

    @Override
    public Employee login(Employee employee) throws EmployeeException {
        /*
         * 员工登录
         * @author 林文塨
         * @date 2022/5/30 9:13
         * @param [employee]
         * @return com.lbmb.mdps.settings.domain.Employee
         */
        Employee employeeVo = employeeDao.login(employee);

        if (employeeVo == null) {
            throw new EmployeeException("账号或者密码错误");
        }

        if ("已禁用".equals(employeeVo.getState())) {
            throw new EmployeeException("账号状态已禁用，请联系管理员！");
        }

        return employeeVo;
    }

    @Override
    public List<Employee> employeePageList(EmployeeReceive employeeReceive) throws EmployeeException {
        /*
         * 分页获取员工表功能
         * @author 林文塨
         * @date 2022/5/30 10:24
         * @param [employeeReceive]
         * @return java.util.List<com.lbmb.mdps.settings.domain.Employee>
         */
        List<Employee> employeeList = employeeDao.employeePageList(employeeReceive);

        if (employeeList == null) {
            throw new EmployeeException("分页获取员工列表失败！");
        }

        return employeeList;
    }

    @Override
    public Integer getEmployeeTotal(EmployeeReceive employeeReceive) throws EmployeeException {
        /*
         * 获取员工分页总条数功能
         * @author 林文塨
         * @date 2022/5/30 10:42
         * @param [employeeReceive]
         * @return java.lang.Integer
         */
        Integer total = employeeDao.getEmployeeTotal(employeeReceive);

        if (total < 0) {
            throw new EmployeeException("获取员工分页总条数失败！");
        }

        return total;
    }

    @Override
    public void addEmployee(Employee employee) throws EmployeeException {
        /*
         * 添加员工
         * @author 林文塨
         * @date 2022/5/30 13:06
         * @param [employee]
         * @return void
         */
        int result = employeeDao.addEmployee(employee);

        if (result != 1) {
            throw new EmployeeException("添加员工失败！");
        }
    }

    @Override
    public void editEmployee(Employee employee) throws EmployeeException {
        /*
         * 修改员工
         * @author 林文塨
         * @date 2022/5/30 13:51
         * @param [employee]
         * @return void
         */
        int result = employeeDao.editEmployee(employee);

        if (result != 1) {
            throw new EmployeeException("修改员工失败！");
        }
    }

    @Override
    public void deleteEmployee(String[] deleteEmployeeIds) throws EmployeeException {
        /*
         * 删除员工
         * @author 林文塨
         * @date 2022/5/30 16:32
         * @param [deleteEmployeeIds]
         * @return void
         */
        int result = employeeDao.deleteEmployee(deleteEmployeeIds);

        if (result <= 0) {
            throw new EmployeeException("删除员工失败");
        }
    }

    @Override
    public void acceptEmployee(String[] editEmployeeStatusIds) throws EmployeeException {
        int result = employeeDao.acceptEmployee(editEmployeeStatusIds);

        if (result <= 0) {
            throw new EmployeeException("启用员工失败！");
        }
    }

    @Override
    public void forbiddenEmployee(String[] editEmployeeStatusIds) throws EmployeeException {
        int result = employeeDao.forbiddenEmployee(editEmployeeStatusIds);

        if (result <= 0) {
            throw new EmployeeException("禁用员工失败！");
        }
    }

    @Override
    public List<EmployeeVo> getAllEmployee() {
        /*
         * 获得全部员工
         * @author 林文塨
         * @date 2022/6/1 1:30
         * @param []
         * @return java.util.List<com.lbmb.mdps.commons.domain.WarehouseVo>
         */
        List<Employee> employeeList = employeeDao.getAllEmployee();

        List<EmployeeVo> employeeVoList = new ArrayList<>();

        for (Employee employee : employeeList) {
            EmployeeVo employeeVo = new EmployeeVo();
            employeeVo.setLabel(employee.getName());
            employeeVo.setValue(employee.getId());

            employeeVoList.add(employeeVo);
        }

        return employeeVoList;
    }

    @Override
    public void editPassword(UserReceive userReceive) throws EmployeeException {
        /*
         * 员工修改密码
         * @author 林文塨
         * @date 2022/6/1 19:41
         * @param [userReceive]
         * @return void
         */

        userReceive.setOldPassword(Md5Util.getMd5(userReceive.getOldPassword()));
        Employee employee = employeeDao.validatePasswordById(userReceive);

        if (employee == null) {
            throw new EmployeeException("旧密码错误！");
        }

        userReceive.setNewPassword(Md5Util.getMd5(userReceive.getNewPassword()));

        int result = employeeDao.editPassword(userReceive);

        if (result != 1) {
            throw new EmployeeException("修改密码失败！");
        }
    }
}
